#include"Search.hpp"
#include"../Common/Log.hpp"
#include"./source/http/http.hpp"
#include "Daemon.hpp"
static const int PORT = 8080;
static std::string BASE_DIR  = "./WWWROOT";
static const std::string input = "/home/ubuntu/Lhd/Project/Boost_Search_Engines/data/output/raw.txt";
static Searcher util_search;
using namespace ns_log;
void Search(const HttpRequest &req, HttpResponse *rsp) 
{
    if(!req.HasParam("word"))
    {
        rsp->SetContent("","text/plain; charset=utf-8");
        return;
    }
    //获取关键字
    std::string word = req.GetParam("word");
    // std::cout<<"用户搜索: "<<word<<std::endl;
    std::string out;
    if(!util_search.Search(word,&out))
    {
         rsp->SetContent("","text/plain; charset=utf-8");
         return;
    }
    rsp->SetContent(out,"application/json");
    return;
}
int main()
{
    Daemon(false,true);
    EnabelFILTRATION();
    util_search.InitSearch(input);
    HttpServer server(PORT);
    server.SetBaseDir(BASE_DIR);
    server.SetThreadCount(10);
    server.Get("/search",Search);
    server.Listen();
    return 0;
}